<?php

namespace Database\Seeders;

use App\Models\ClusterReport;
use App\Models\ESBattery;
use App\Models\ESBatteryCell;
use App\Models\ESCluster;
use App\Models\ESClusterChargeLog;
use App\Models\ESStation;
use App\Models\ESUnit;

use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Http;

class InitESStationSeeder extends Seeder
{
    public function fill_lnglat(): void
    {
        $stations = ESStation::all();
        foreach($stations as $station){
            $address = $station->zone . $station->address;
            $response = Http::retry(2, 100)->get('https://restapi.amap.com/v3/geocode/geo', [
                'key' => 'bbdc9ebbaa012ed980b829154111e512',
                'address' => $address ,
            ]);
            if($response->successful()){
                dump($response['geocodes']);
                $loc = explode(',', $response['geocodes'][0]['location']);
                if (isset($loc[1])){
                    $station->lng = $loc[0];
                    $station->lat = $loc[1];
                    $station->save();    
                }
            }
        }
    }
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        ESStation::factory(10)->create();
        ESUnit::factory(10)->create();
        ESCluster::factory(50)->create();
        ESClusterChargeLog::factory(100)->create();
        ESBattery::factory(50)->create();
        // ESBatteryCell::factory(10)->create();
        ClusterReport::factory(100)->create();
        $this->fill_lnglat();
    }
}
